﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Aludra.Core.Models
{
    public class HandZone
    {
        private List<ICard> _onHandCards;

        #region IField Members
        public FieldType FieldType
        {
            get { return FieldType.HandZone; }
        }

        public int Size
        {
            get { return int.MaxValue; }
        }

        public int Count
        {
            get { return _onHandCards.Count; }
        }

        public event EventHandler<CardsRemovedEventArgs> CardsRemoved;

        public event EventHandler<CardsAddedEventArgs> CardsAdded;

        #endregion

        public HandZone(IEnumerable<ICard> cards)
        {
            _onHandCards = new List<ICard>(cards);
        }

        public void Remove(ICard item)
        {
            _onHandCards.Remove(item);
            // Copy to a temporary variable to be thread-safe.
            EventHandler<CardsRemovedEventArgs> temp = CardsRemoved;
            if (temp != null)
            {
                temp(this, new CardsRemovedEventArgs(this.FieldType, new[] { item }));
            }
        }

        public ICard this[int index]
        {
            get
            {
                return _onHandCards[index];
            }
        }

        public void Add(ICard item)
        {
            _onHandCards.Add(item);

            EventHandler<CardsAddedEventArgs> tempAdded = this.CardsAdded;
            if (tempAdded != null)
            {
                tempAdded(this, new CardsAddedEventArgs(this.FieldType, new[] { item }));
            }
        }

        public void AddRange(IEnumerable<ICard> cards)
        {
            _onHandCards.AddRange(cards);

            EventHandler<CardsAddedEventArgs> tempAdded = this.CardsAdded;
            if (tempAdded != null)
            {
                tempAdded(this, new CardsAddedEventArgs(this.FieldType, cards));
            }
        }

        public void Clear()
        {
            // Copy to a temporary variable to be thread-safe.
            EventHandler<CardsRemovedEventArgs> temp = CardsRemoved;
            if (temp != null)
            {
                temp(this, new CardsRemovedEventArgs(this.FieldType, _onHandCards));
            }
            _onHandCards.Clear();
        }

        public bool Contains(ICard item)
        {
            return _onHandCards.Contains(item);
        }
    }
}
